<?php


session_start();
require_once("../include/MySugarApplication.php");
require_once("../include/ContactSoap.php");
$app->checkSession('Contacts/ContactsList.php');

$id = $_REQUEST["id"];

if ($_REQUEST["actionType"] == "DELETE") {

	$result=$contactSoap->deleteContact($id);

} else {

$errors = array();


if ($_REQUEST["actionType"] == "UPDATE" && $id == "")
	$errros[] = "Cannot update when primary key is not passed";

if ($_FILES["photo"]["error"] > 0) {
 	$errors[] = "File upload error: " . $_FILES["file"]["error"];
 } else {
 
  $target_path = $_SERVER['DOCUMENT_ROOT']."/images/mysugar/".$id;
  
  if(move_uploaded_file($_FILES['photo']['tmp_name'], $target_path)) {
  		$photo_c = "/images/mysugar/".$id;
	} else{
		$errors = "There was an error uploading the file, please try again!";
	}
 	
 }


$salutation = $_REQUEST["salutation"];

if ($salutation == "")
	$errors[] = "Salutation is mandatory";

$first_name = $_REQUEST["first_name"];
if ($first_name == "")
	$errors[] = "First name is mandatory";

$last_name = $_REQUEST["last_name"];
if ($last_name == "")
	$errors[] = "Last name is mandatory";
	
$account_id = $_REQUEST["account_id"];
$lead_source = $_REQUEST["load_source"];
$campaign_id = $_REQUEST["compaign_id"];
$title = $_REQUEST["title"];
$department = $_REQUEST["department"];
$assigned_user_id = $_REQUEST["assigned_user_id"];
$email1 = $_REQUEST["email1"];
$phone_work = $_REQUEST["phone_work"];
$phone_mobile = $_REQUEST["phone_mobile"];
$phone_home = $_REQUEST["phone_home"];
$phone_other = $_REQUEST["phone_other"];
$phone_fax = $_REQUEST["phone_fax"];
$do_not_call = $_REQUEST["do_not_call"];
$primary_address_street = $_REQUEST["primary_address_street"];
$primary_address_city = $_REQUEST["primary_address_city"];
$primary_address_state = $_REQUEST["primary_address_state"];
$primary_address_postalcode = $_REQUEST["primary_address_postalcode"];
$primary_address_country = $_REQUEST["primary_address_country"];
$description = $_REQUEST["description"];
$date_modified = $_REQUEST["last_modified_on"];




if (count($errors) == 0) {
	$result=$contactSoap->saveContact($id, $photo_c, $salutation, $first_name, $last_name,
 								  	  $account_id, $lead_source, $campaign_id,
								      $title, $department, $assigned_user_id,
								  	$email1, $phone_work, $phone_mobile, $phone_home, $phone_other,
								  	$phone_fax, $do_not_call, $primary_address_street,
								  	$primary_address_city, $primary_address_state,
								  	$primary_address_postalcode, 
								  	$primary_address_country, $description,
								  	$date_modified);
}	     		  

}


?>
<!DOCTYPE HTML SYSTEM "http://www.bitzermobile.com/bitzer/dtd/HTML401-loose-bitzer.dtd">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
		<?php
		if (!$isBitzer) {
        	echo '
			<title>MySugar Contacts</title>
			<LINK REL=StyleSheet HREF="',$app->appRoot,'include/style.css" TYPE="text/css" MEDIA=screen>
        <script type="text/javascript" src="',$app->appRoot,'include/niftycube.js"></script>
        <script type="text/javascript">
            window.onload=function(){
				var width = "550px";
				setAppRoot("',$app->appRoot,'");
                Nifty("div#container");
                Nifty("div#content,div#nav","same-height small");
                Nifty("div#header,div#footer","small");
                Nifty("ul.postnav a","transparent");
				setWidth("div#container", width);
				setWidth("div#content", width);
				setWidth("div#footer", width);
            }
        </script>';
        }
			?>    
    </head>
    <body>
    	<?php 
		if (count($errors) == 0) {
			if (!$isBitzer) {
        		echo '<div id="container">
            		<div id="header">
            			<table width="100%">
							<tr>
								<td width="95%"><h1>Contacts Saved</h1></td>
								<td align="right" width="5%"><a href="',$app->appRoot,'Logout.php">Logout</a></td>
							</tr>
						</table>
            		</div>
            		<div id="content">
                	<br>';
			}
			echo '<table data-bitzer-result="success" class="list" width="100%">
                  	<tr><th>Saved</th>
                    <tr><td data-bitzer-primary-key-name="id">',$result["id"],'</td></tr>    
                  </table>';
			if (!$isBitzer) {
				echo '
                <br>
                    <table>
                        <tr><td>
                                <ul class="postnav">
                                    <li><a href="ContactsList.php">List</a></li>
                                </ul>
                            </td>
                        </tr>
					</table>
            </div>
            <div id="footer"></div>
        </div>';
			}
		} else {
			if (!$isBitzer) {
        		echo '<div id="container">
            		<div id="header">
            			<table width="100%">
							<tr>
								<td width="95%"><h1>Contact Save Errors</h1></td>
								<td align="right" width="5%"><a href="/mysugar/Logout.php">Logout</a></td>
							</tr>
						</table>
            		</div>
            		<div id="content">
                	<br>';
			}
			echo '<table data-bitzer-result="error" ';
			if (!$isBitzer) {
				echo 'class="list" width="100%"';
			}
			echo '>
                        <tr><th>Errors</th>';
			foreach ($errors as $key => $error) {
				echo '<tr><td data-bitzer-field="',$key,'"><font color="#ff0000">',$error,'</font></td></tr>';
			}
			echo '</table>';
			
            if (!$isBitzer) {
               echo '
                <br>
                    <table>
                        <tr><td>
                                <ul class="postnav">
                                    <li><a href="ContactsList.php">List</a></li>
                                </ul>
                            </td>
                        </tr>
					</table>
            </div>
            <div id="footer"></div>
        </div>';
			}

		}
		?>
    </body>
</html>
